package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/mars/internal/admin/core/adapter"
	"github.com/mars/internal/admin/core/domain"

	"github.com/mars/internal/admin/middlewares"
	"github.com/mars/internal/pkg/gins"
)

func initRouter(g *gin.Engine, domainFactory domain.Factory) {
	initMiddlerwares(g)

	adminGroup := g.Group("/api/v1/admin")
	adminGroup.Use(middlewares.ValidateAuth())
	installController(adminGroup, domainFactory)
}

func initMiddlerwares(g *gin.Engine) {
	g.Use(middlewares.Cors())
	g.Use(middlewares.RequestID())
	g.Use(middlewares.Logger())
}

func installController(adminGroup *gin.RouterGroup, domainFactory domain.Factory) {

	{
		userC := adapter.NewUserController(domainFactory)
		userGroup := adminGroup.Group("/user")

		// 查询相关接口
		userGroup.GET("/list", gins.HandlePageRestWrapper(userC.GetUserList))
		userGroup.GET("/detail/:openid", gins.HandleRestWrapper(userC.GetUser))
		userGroup.GET("/detail", gins.HandleRestWrapper(userC.GetUser)) // 支持query参数
		userGroup.GET("/status/:status", gins.HandleRestWrapper(userC.GetUsersByStatus))
		userGroup.GET("/status", gins.HandleRestWrapper(userC.GetUsersByStatus)) // 支持query参数
		userGroup.GET("/vip", gins.HandleRestWrapper(userC.GetVIPUsers))
		userGroup.GET("/stats", gins.HandleRestWrapper(userC.GetUserStats))

		// 操作相关接口（需要权限验证）
		userPermissionGroup := userGroup.Use(middlewares.PermissionValidate())
		userPermissionGroup.POST("/create", gins.HandleRestWrapper(userC.CreateUser))
		userPermissionGroup.PUT("/update/:openid", gins.HandleRawResponseWrapper(userC.UpdateUser))
		userPermissionGroup.PUT("/update", gins.HandleRawResponseWrapper(userC.UpdateUser)) // 支持query参数
		userPermissionGroup.PUT("/vip/:openid", gins.HandleRestWrapper(userC.UpdateUserVIP))
	}

	{
		bookingC := adapter.NewBookingController(domainFactory)
		bookingGroup := adminGroup.Group("/booking")

		bookingGroup.GET("/list", gins.HandlePageRestWrapper(bookingC.GetBookingList))
		bookingGroup.GET("/detail/:bookingId", gins.HandleRestWrapper(bookingC.GetBookingDetail))
		bookingGroup.GET("/stats", gins.HandleRestWrapper(bookingC.GetBookingStats))

		// 操作相关接口（需要权限验证）
		bookingPermissionGroup := bookingGroup.Use(middlewares.PermissionValidate())
		bookingPermissionGroup.PUT("/confirm/:bookingId", gins.HandleRawResponseWrapper(bookingC.ConfirmBooking))
		bookingPermissionGroup.PUT("/reject/:bookingId", gins.HandleRawResponseWrapper(bookingC.RejectBooking))
		bookingPermissionGroup.PUT("/complete/:bookingId", gins.HandleRawResponseWrapper(bookingC.CompleteBooking))
		// bookingPermissionGroup.PUT("/cancel/:bookingId", gins.HandleRawResponseWrapper(bookingC.CancelBooking))
	}

	{
		productC := adapter.NewProductController(domainFactory)
		productGroup := adminGroup.Group("/product")

		// 查询相关接口
		productGroup.GET("/list", gins.HandlePageRestWrapper(productC.GetProductList))
		productGroup.GET("/detail/:id", gins.HandleRestWrapper(productC.GetProduct))
		// productGroup.GET("/type/:type", gins.HandleRestWrapper(productC.GetProductsByType))
		// productGroup.GET("/status/:status", gins.HandleRestWrapper(productC.GetProductsByStatus))
		productGroup.GET("/stats", gins.HandleRestWrapper(productC.GetProductStats))
		// productGroup.GET("/search", gins.HandleRestWrapper(productC.SearchProducts))

		// 操作相关接口（需要权限验证）
		productPermissionGroup := productGroup.Use(middlewares.PermissionValidate())
		productPermissionGroup.POST("/create", gins.HandleRawResponseWrapper(productC.CreateProduct))
		productPermissionGroup.PUT("/update/:id", gins.HandleRawResponseWrapper(productC.UpdateProduct))
		productPermissionGroup.DELETE("/delete/:id", gins.HandleRestWrapper(productC.DeleteProduct))
		productPermissionGroup.POST("/generate-upload-urls", gins.HandleRestWrapper(productC.GenerateUploadUrls))
		productPermissionGroup.POST("/upload-images", gins.HandleRestWrapper(productC.UploadProductImage))
		// productPermissionGroup.PUT("/status/:id", gins.HandleRawResponseWrapper(productC.UpdateProductStatus))
		// productPermissionGroup.PUT("/stock/:id", gins.HandleRawResponseWrapper(productC.UpdateProductStock))
	}

	{
		cartC := adapter.NewCartController(domainFactory)
		cartGroup := adminGroup.Group("/cart")

		// 所有购物车接口只允许查询，管理员不能执行任何操作
		cartGroup.GET("/list", gins.HandlePageRestWrapper(cartC.GetCartList))
		cartGroup.GET("/detail/:id", gins.HandleRestWrapper(cartC.GetCartItem))
		cartGroup.GET("/user/:openid", gins.HandleRestWrapper(cartC.GetUserCartItems))
		// cartGroup.GET("/product/:productId", gins.HandleRestWrapper(cartC.GetCartItemsByProduct))
		// cartGroup.GET("/status/:status", gins.HandleRestWrapper(cartC.GetCartItemsByStatus))
		// cartGroup.GET("/stats", gins.HandleRestWrapper(cartC.GetCartStats))
		// cartGroup.GET("/search", gins.HandleRestWrapper(cartC.SearchCartItems))
	}

	{
		orderC := adapter.NewOrderController(domainFactory)
		orderGroup := adminGroup.Group("/order")

		// 查询相关接口
		orderGroup.GET("/list", gins.HandlePageRestWrapper(orderC.GetOrderList))
		orderGroup.GET("/detail/:id", gins.HandleRestWrapper(orderC.GetOrder))
		orderGroup.GET("/stats", gins.HandleRestWrapper(orderC.GetOrderStats))
		orderGroup.GET("/search", gins.HandleRestWrapper(orderC.SearchOrders))
		orderGroup.GET("/status/:status", gins.HandleRestWrapper(orderC.GetOrdersByStatus))
		orderGroup.GET("/payment/:method", gins.HandleRestWrapper(orderC.GetOrdersByPaymentMethod))
		orderGroup.GET("/type/:type", gins.HandleRestWrapper(orderC.GetOrdersByProductType))
		orderGroup.GET("/vip", gins.HandleRestWrapper(orderC.GetVipOrders))
		orderGroup.GET("/discounts", gins.HandleRestWrapper(orderC.GetOrdersWithDiscounts))
		orderGroup.GET("/export", gins.HandleRestWrapper(orderC.ExportOrders))

		// 操作相关接口（需要权限验证）
		orderPermissionGroup := orderGroup.Use(middlewares.PermissionValidate())
		orderPermissionGroup.PUT("/status/:id/:status", gins.HandleRawResponseWrapper(orderC.UpdateOrderStatus))
		orderPermissionGroup.POST("/refund/:id", gins.HandleRawResponseWrapper(orderC.ProcessRefund))
	}

	{
		debugC := adapter.NewDebugController()
		debugGroup := adminGroup.Group("/debug")

		debugGroup.GET("/collection", gins.HandleRestWrapper(debugC.GetCollectionDataHandler))
	}

	// {
	// 	proposalC := proposal.NewProposalController(factory)
	// 	proposalGroup := adminGroup.Group("/proposal")

	// 	proposalGroup.GET("/fileinfo", gins.HandleRestWrapper(proposalC.QueryFileInfo))
	// 	proposalGroup.GET("/list", gins.HandlePageRestWrapper(proposalC.QueryAllProposal))
	// 	proposalGroup.GET("/listByIds/:taskId", gins.HandleRestWrapper(proposalC.QueryProposalByTaskIds))
	// }

	// {
	// 	appC := app.NewAppController(factory)
	// 	appGroup := adminGroup.Group("/app")

	// 	appGroup.GET("/detail", gins.HandleRestWrapper(appC.AppDetail))
	// 	appGroup.GET("/list", gins.HandlePageRestWrapper(appC.QueryApps))

	// 	appPermissionGroup := appGroup.Use(middlewares.PermissionValidate())
	// 	appPermissionGroup.PUT("/create-bucket/:bucketName", gins.HandleErrorWrapper(appC.CreateBucket))
	// 	appPermissionGroup.PUT("/create-app", gins.HandleErrorWrapper(appC.CreateAppV2))
	// 	appPermissionGroup.PATCH("/update-app", gins.HandleErrorWrapper(appC.UpdateApp))

	// 	// 为了兼容存量的逻辑
	// 	appPermissionGroup.POST("/create-app/:appName", gins.HandleErrorWrapper(appC.CreateApp))
	// 	appPermissionGroup.POST("/create-app/v2/:appName", gins.HandleErrorWrapper(appC.CreateAppV2))
	// 	appPermissionGroup.DELETE("/delete-app/:appName/:bucketName", gins.HandleErrorWrapper(appC.DeleteApp))
	// 	appPermissionGroup.DELETE("/delete-app/:appName", gins.HandleErrorWrapper(appC.DeleteApp))
	// 	appPermissionGroup.PUT("/update-app/:appName", gins.HandleErrorWrapper(appC.UpdateApp))
	// 	appPermissionGroup.PUT("/update-app-resource/:appName", gins.HandleErrorWrapper(appC.UpdateAppResource))
	// 	appPermissionGroup.PATCH("/switch-app-status/:appName", gins.HandleErrorWrapper(appC.SwitchAppStatus))
	// 	appPermissionGroup.PATCH("/switch-app-alert/:appName", gins.HandleErrorWrapper(appC.SwitchAppAlert))
	// 	appPermissionGroup.PATCH("/switch-app-sync/:appName", gins.HandleErrorWrapper(appC.SwitchAppSync))
	// 	appPermissionGroup.PATCH("/switch-app-ceph-sync/:appName", gins.HandleErrorWrapper(appC.SwitchAppCephSync))
	// 	appPermissionGroup.PATCH("/switch-app-auto-restart/:appName", gins.HandleErrorWrapper(appC.SwitchAppAutoRestart))
	// 	appPermissionGroup.PATCH("/switch-app-force-restart/:appName", gins.HandleErrorWrapper(appC.SwitchAppForceRestart))
	// 	appPermissionGroup.PATCH("/switch-app-hot-push/:appName", gins.HandleErrorWrapper(appC.SwitchAppHotPush))
	// 	appPermissionGroup.PATCH("/switch-app-master-slave/:appName", gins.HandleErrorWrapper(appC.SwitchAppMasterSlave))

	// 	// 为了兼容存量的逻辑
	// 	appPermissionGroup.POST("/create", gins.HandleErrorWrapper(appC.CreateApp))
	// 	appPermissionGroup.DELETE("/delete/:appName", gins.HandleErrorWrapper(appC.DeleteApp))
	// 	appPermissionGroup.PUT("/update/:appName", gins.HandleErrorWrapper(appC.UpdateApp))
	// 	appPermissionGroup.PATCH("/switch-status/:appName", gins.HandleErrorWrapper(appC.SwitchAppStatus))
	// 	appPermissionGroup.PATCH("/switch-alert/:appName", gins.HandleErrorWrapper(appC.SwitchAppAlert))
	// 	appPermissionGroup.PATCH("/switch-sync/:appName", gins.HandleErrorWrapper(appC.SwitchAppSync))
	// 	appPermissionGroup.PATCH("/switch-auto-restart/:appName", gins.HandleErrorWrapper(appC.SwitchAppAutoRestart))
	// 	appPermissionGroup.PATCH("/switch-force-restart/:appName", gins.HandleErrorWrapper(appC.SwitchAppForceRestart))
	// 	appPermissionGroup.PATCH("/switch-hot-push/:appName", gins.HandleErrorWrapper(appC.SwitchAppHotPush))
	// 	appPermissionGroup.PATCH("/switch-master-slave/:appName", gins.HandleErrorWrapper(appC.SwitchAppMasterSlave))
	// }

	// {
	// 	taskC := task.NewTaskController(factory)
	// 	taskGroup := adminGroup.Group("/task")

	// 	taskGroup.GET("/list", gins.HandlePageRestWrapper(taskC.QueryTasks))
	// 	taskGroup.GET("/detail/:taskId", gins.HandleRestWrapper(taskC.QueryTask))
	// 	taskGroup.GET("/restart/:taskId", gins.HandleRestWrapper(taskC.RestartTask))
	// 	taskGroup.GET("/stop/:taskId", gins.HandleRestWrapper(taskC.StopTask))
	// 	taskGroup.DELETE("/delete/:taskId", gins.HandleRestWrapper(taskC.DeleteTask))

	// 	taskPermissionGroup := taskGroup.Use(middlewares.PermissionValidate())
	// 	taskPermissionGroup.POST("/create", gins.HandleRestWrapper(taskC.CreateTask))
	// 	taskGroup.POST("/create-oss-task", gins.HandleRestWrapper(taskC.CreateOssTask))
	// 	taskGroup.POST("/create-ceph-task", gins.HandleRestWrapper(taskC.CreateCephTask))
	// 	taskGroup.POST("/create-bucket", gins.HandleRestWrapper(taskC.CreateBucket))
	// 	taskGroup.PATCH("/update/:taskId", gins.HandleRestWrapper(taskC.UpdateTask))
	// 	taskGroup.PATCH("/update-time/:taskId", gins.HandleRestWrapper(taskC.UpdateTimeTask))
	// 	taskGroup.PATCH("/switch-status/:taskId", gins.HandleRestWrapper(taskC.SwitchTaskStatus))
	// }
}
